package calendar.demo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.LinearLayout.LayoutParams;

public class TimeCell extends View {
	
	public interface OnTimeCellClick {
		public void OnClick(TimeCell timeCell);
	}
	
	private OnTimeCellClick timeCellClick = null;  
	private Paint graphPaint = null;
	private Paint borderPaint = null;
	private Paint textPaint = null;

	private boolean isFocused = false;
	private boolean isSelected = false;

	private final int width;
	private final int height;
	
	public TimeCell(Context context, int width, int height) {
		super(context);
		this.setFocusableInTouchMode(true);
		this.setFocusable(true);  
		this.width = width;
		this.height = height;
		setLayoutParams(new LayoutParams(width, height));  
		init();
	}
	
	public void setOnTimeCellClick (OnTimeCellClick timeCellClick) {
		this.timeCellClick = timeCellClick;
	}

	public void setTimeCellText(String text) {
	}
	
	private void init() {
		//图形画笔
		graphPaint = new Paint();
		//设置画笔颜色 透明度
		graphPaint.setARGB(255, 251, 251, 251);
		//去锯齿
		graphPaint.setAntiAlias(true);
		//抖动处理 
		graphPaint.setDither(true);
		// 设置空心实心
		graphPaint.setStyle(Style.FILL);
		
		//图形画笔
		borderPaint = new Paint();
		borderPaint.setARGB(255, 100, 100, 100);
		borderPaint.setAntiAlias(true);
		borderPaint.setDither(true);
		borderPaint.setStyle(Style.STROKE);
		borderPaint.setStrokeWidth(1);
		
		//设置文本画笔
		textPaint = new Paint();
		//设置文字颜色
		textPaint.setARGB(255, 115, 115, 115);
		
		textPaint.setSubpixelText(true);
		//设置绘制文字的对齐方向
		textPaint.setTextAlign(Align.CENTER);
		//设置绘制文字的字号大小
		textPaint.setTextSize(12);
	}
	
	private void doTimeCellClick() {
		if (timeCellClick != null) {
			timeCellClick.OnClick(this);
		}
	}
	  
	//绘制单元格
	private void drawGraph(Canvas canvas) {
		
		if (isFocused || isSelected) {
			graphPaint.setAlpha(200);
		} else {
			graphPaint.setAlpha(255);
		}
		
		canvas.drawRect(0, 0, width, height, graphPaint);
		canvas.drawRect(0, 0, width, height, borderPaint);
	}
	
	//绘制文本
	private void drawText(Canvas canvas) {
		
	}

	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		drawGraph(canvas);
		drawText(canvas);
	}
	
	@Override
	public void setSelected(boolean selected) {
		this.isSelected = selected;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean result =  super.onKeyDown(keyCode, event);
		
		if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER)  || (keyCode == KeyEvent.KEYCODE_ENTER)) {  
			doTimeCellClick();
		}
		
		return result;
	}
	
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		return super.onKeyUp(keyCode, event);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		  boolean result = false;  
	        if (event.getAction() == MotionEvent.ACTION_DOWN) {  
	        	result = true;  
	            startAlphaAnimIn(TimeCell.this);  
	        }  
	        if (event.getAction() == MotionEvent.ACTION_CANCEL) {  
	        	result = true;  
	        }  
	        if (event.getAction() == MotionEvent.ACTION_UP) {  
	        	result = true;   
	            doTimeCellClick();  
	        }  
	        return result;  
	}

	@Override
	protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
		// 鼠标点击无法触发该事件
		isFocused = gainFocus;
		super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
	}

    public static void startAlphaAnimIn(View view) {  
        AlphaAnimation anim = new AlphaAnimation(0.5F, 1);  
        anim.setDuration(100);  
        anim.startNow();  
        view.startAnimation(anim);  
    }  

}
